<?php
/**
 * Created by PhpStorm.
 * User: 丶晓
 * Date: 2017/8/26
 * Time: 19:43
 */

namespace app\home\controller;

use app\api\controller\Wechat;
use app\common\Base;
use think\Db;
use think\Session;
use think\Controller;
use app\home\common\Tools;
use app\home\service\SetService;
use app\api\service\WechatService;
use app\home\service\AccountService;

class System extends Controller
{

    private $MINI_ID;
    private $WX_id;
    private $WX_secret;
    private $TEMP_ID;

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $A = new AccountService();
        $this->MINI_ID = Session::get('login_miniID');
        $this->TEMP_ID = Session::get('login_tplID');

        $this->WX_id = $A->getminiAppsData('appid', $this->MINI_ID);
        $this->WX_secret = $A->getminiAppsData('secret', $this->MINI_ID);
    }

    public function index()
    {
        $CONFIG = [
            'title' => '设置',
            'js' => [
                'home/js/plugins/sparkline/jquery.sparkline.min.js'
            ],
            'path' => [
                '设置'
            ],

        ];
        $this->assign('CONFIG', $CONFIG);

//        $A = new Tools();
//        $data = $this->getExpire();
//        if (!$data) {
//            return $A->error('~服务器开小差啦，请联系程序猿修复...');
//        }
//
//
//        $this->assign('data', $data);
        return $this->fetch('index');
    }

    public function getExpire()
    {

        $data['miniapps_id'] = $this->MINI_ID;
        $set = new SetService();
        $res = $set->getMiniInfo('uid_name', $data);

        if (!$res) {
            return false;
        }
        $arr['uid'] = $res[0]['uid'];
        $arr['template_id'] = $this->TEMP_ID;

        $result = $set->getMiniInfo('time_price', $arr);
        if (!$result) {
            return false;
        }

        $result[0]['name'] = $res[0]['name'];
        $aa = $result[0]['add_time'];
        $over_year = substr($aa, 0, 4) + 1;

        $over_other = substr($aa, 4);
        $over_time = $over_year . "" . $over_other;
        $result[0]['over_time'] = $over_time;

        return $result;
    }

    public function bindWechatMsg()
    {
        $Wechat = new Wechat();
        $RES = json_decode($Wechat->getQrcode('setcallback', $this->MINI_ID),true);
        return $RES['state'] ? $RES['data']['src'] : 'request fail!';
    }

    public function get_app()
    {
        return 'http://qr.liantu.com/api.php?text=https://res.suyongw.com/suYongCloudApp.apk';
    }

    public function select_tpl()
    {
        $res = Db::table('wyz_template')->where('template_id',$this->TEMP_ID)->value('tpl');
        if($res == 8){
            return true;
        }else{
            return false;
        }
    }

}